package com.gitdog.lllang.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

public class LLFileReader {
	
	/*
	 * 这个文件呢，是从jasic那个抄了一些代码过来，不过也还是做了一些修改，比如增加了getResource方法
	 * 所以连人家的注释都没去掉
	 * jasic是个好项目，一个文件解释了basic 
	 * 
	 * */
	
	public static String getResouce(String path){
		return get(Thread.currentThread().getContextClassLoader().getResource("").getPath() + path);
	}
	
	public static String get(String path) {
        try {
            FileInputStream stream = new FileInputStream(path);
            
            try {
                InputStreamReader input = new InputStreamReader(stream,
                    Charset.defaultCharset());
                Reader reader = new BufferedReader(input);
                
                StringBuilder builder = new StringBuilder();
                char[] buffer = new char[8192];
                int read;
                
                while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
                    builder.append(buffer, 0, read);
                }
                
                // HACK: The parser expects every statement to end in a newline,
                // even the very last one, so we'll just tack one on here in
                // case the file doesn't have one.
                builder.append("\n");
                
                return builder.toString();
            } finally {
                stream.close();
            }
        } catch (IOException ex) {
            return null;
        }
    }
	
}
